package com.atguigu.tingshu.order.consumer;

import com.atguigu.tingshu.common.constant.RabbitConstant;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Haoran Sun
 * @since 2024/08/29
 */

@Component
@Slf4j
public class OrderSuccessConsumer {

    @Resource
    private OrderInfoService orderInfoService;
    @RabbitListener(queues = RabbitConstant.ORDER_CANCEL_QUEUE)
    public void orderCancelConsumer(String orderNo, Channel channel, Message message) throws IOException {

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try{
            orderInfoService.successOrder(orderNo);
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            channel.basicReject(deliveryTag, !message.getMessageProperties().getRedelivered());
        }
    }
}
